package com.ambow.servlet;

import java.io.IOException;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ambow.Entity.DeliverToStation;
import com.ambow.Entity.DeliverToTeacher;
import com.ambow.Entity.Operator;
import com.ambow.Entity.Station;
import com.ambow.Entity.Stock;
import com.ambow.Entity.TextBook;
import com.ambow.service.DeliverToStationService;
import com.ambow.service.StationService;
import com.ambow.service.StockService;
import com.ambow.service.TextBookService;
import com.ambow.service.impl.DeliverToStationServiceImpl;
import com.ambow.service.impl.StationServiceImpl;
import com.ambow.service.impl.StockServiceImpl;
import com.ambow.service.impl.TextBookServiceImpl;
import com.ambow.utils.BaseDAO;
import com.ambow.utils.HibernateUtil;



/**
 * Servlet implementation class DeliverToStation
 */
@WebServlet("/DeliverToStationServlet")
public class DeliverToStationServlet extends HttpServlet {
	DeliverToStationService dService=new DeliverToStationServiceImpl();
	TextBookService tService=new TextBookServiceImpl();
	StationService sService=new StationServiceImpl();
	StockService stockService=new StockServiceImpl();
	Date date=new Date();
	Operator operator=new Operator();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			doPost(request,response);
		
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String status=request.getParameter("status");
		if(status.equals("query")) {
			query(request,response);
		}
		else if(status.equals("add")){
			add(request,response);
		}else if(status.equals("querybook")) {
			queryBook(request,response);
		}
		
	}

	private void queryBook(HttpServletRequest request, HttpServletResponse response) {//函授站领书查询
		// 函授站领书查询
		try {
			String deliverDatefrm=request.getParameter("deliverDatefrm");
			String deliverDateto=request.getParameter("deliverDateto");
			String staionName=request.getParameter("stationName");//函授站
			System.out.println(staionName);
			if(staionName!=null&&!staionName.isEmpty()) {
				String hql="from DeliverToStation station_id where (deliverDate BETWEEN ? and ?) and station_id in (select id from Station where name=?)";
				String []param= {deliverDatefrm,deliverDateto,staionName};
				List<DeliverToStation> deliverbooks=dService.queryDeliverStationbook(hql, param);
				request.setAttribute("deliverbooks", deliverbooks);
				request.getRequestDispatcher("pages/module/storage/storage001/station/deliver_query.jsp").forward(request, response);
				}
			else{
				String hql="from DeliverToStation  where  (deliverDate BETWEEN ? and ?) ";
				String []param= {deliverDatefrm,deliverDateto};
				List<DeliverToStation> deliverbooks=dService.queryDeliverStationbook(hql, param);
				request.setAttribute("deliverbooks", deliverbooks);
				request.getRequestDispatcher("pages/module/storage/storage001/station/deliver_query.jsp").forward(request, response);
				}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			Integer amount=Integer.valueOf(request.getParameter("lnum"));//领书数量
			Integer tid=Integer.valueOf(request.getParameter("radio"));//教材id
			Integer sid=Integer.valueOf(request.getParameter("station"));//函授站id
			TextBook tbook=tService.queryOne(TextBook.class, tid);
			Station station=sService.queryBookById(sid);
			DeliverToStation dToStation=new DeliverToStation();
			dToStation.setStation(station);
			dToStation.setLnum(amount);
			dToStation.setDeliverDate(date);
			dToStation.setTextBook(tbook);
			if(request.getSession().getAttribute("op")!=null) {
				System.out.println("123456778");
				dToStation.setOperator((Operator)request.getSession().getAttribute("op"));
			}else {
				System.out.println("12345678dfgggggg");
				dToStation.setOperator((Operator)BaseDAO.get(Operator.class, 1));
			}
			boolean b=dService.addDeliverToStation(dToStation);
			if(b) {
					request.getRequestDispatcher("pages/module/storage/storage001/station/deliver_query.jsp").forward(request, response);
					Integer samount=Integer.valueOf(request.getParameter("amount"));//库存数量
					Integer num=samount-amount;//领书后的数量
					Integer cid=Integer.valueOf(request.getParameter("cell"));//货架id
					Stock stock=stockService.queryStock("from Stock as stock where stock.textBook.id="+tid+"and stock.cell.id="+cid);
					System.out.println(stock);
					stock.setAmount(num);
					boolean c=stockService.update(stock);
					if(c) {
						System.out.println("库存更新成功");
					}else {
						System.out.println("更新失败");
					}
				}else {
					response.sendRedirect("pages/module/storage/storage001/station/station.jsp");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		
	}
	private void query(HttpServletRequest request, HttpServletResponse response) {//教材查询
		// TODO Auto-generated method stub
		try {
			String bookname=request.getParameter("bookname");//教材名称
			String author=request.getParameter("author");//作者
			String publish=request.getParameter("publish");//出版社
			String hql="from Stock textBook_id where textBook_id in (select id from TextBook t where t.name=? and t.author=? and t.publish=? )"	;		
			String []param= {bookname,author,publish};
			Stock stocks=dService.querybook(hql, param);
			System.out.println("查询成功");
			request.setAttribute("stocks", stocks);
			request.getRequestDispatcher("pages/module/storage/storage001/station/book_query.jsp").forward(request, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}
